package org.jtzc.chat.controller;

import org.jtzc.chat.entity.Book;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.converter.BeanOutputConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author wu chuang
 * @description
 */
@RestController
public class PromptsController {

    @Autowired
    private ChatModel chatModel;

    @GetMapping("/template")
    public String promptTemplate(String author){
        final String template = "请问{author}最受欢迎的书是哪本书？什么时候发布的？书的内容是什么？";
        PromptTemplate promptTemplate = new PromptTemplate(template);
        Prompt prompt = promptTemplate.create(Map.of("author", author));

        ChatResponse chatResponse = chatModel.call(prompt);

        AssistantMessage assistantMessage = chatResponse.getResult().getOutput();
        return assistantMessage.getContent();
    }

    @Value("classpath:prompt.st")
    private Resource promptTemplateResource;

    @GetMapping("/template2")
    public String promptTemplate2(String author) {
        PromptTemplate promptTemplate = new PromptTemplate(promptTemplateResource);
        Prompt prompt = promptTemplate.create(Map.of("author", author));
        ChatResponse chatResponse = chatModel.call(prompt);
        AssistantMessage assistantMessage = chatResponse.getResult().getOutput();
        return assistantMessage.getContent();
    }

    @Value("classpath:code.st")
    private Resource codeTemplate;

    @GetMapping("/code")
    public String generateCode(@RequestParam String description, @RequestParam String language, @RequestParam String methodName) {
        PromptTemplate promptTemplate = new PromptTemplate(codeTemplate);
        Prompt prompt = promptTemplate.create(
                Map.of("description", description, "language", language, "methodName", methodName)
        );
        ChatResponse chatResponse = chatModel.call(prompt);
        AssistantMessage assistantMessage = chatResponse.getResult().getOutput();
        return assistantMessage.getContent();
    }

    @GetMapping("/bean")
    public Book getBookByAuthor(String author) {
        final String template = """
                        请告诉我{author}最受欢迎的书是哪本？什么时间出版的？书的内容描述了什么？
                        {format}
                """;
        BeanOutputConverter<Book> bookBeanOutputConverter = new BeanOutputConverter<>(Book.class);
        PromptTemplate promptTemplate = new PromptTemplate(template);
        Prompt prompt = promptTemplate.create(Map.of("author", author, "format", bookBeanOutputConverter.getFormat()));
        ChatResponse chatResponse = chatModel.call(prompt);
        AssistantMessage assistantMessage = chatResponse.getResult().getOutput();
        Book book = bookBeanOutputConverter.convert(assistantMessage.getContent());
        return book;

    }
}
